package 数组.最长递增子序列;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/5/5 16:17
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        int[] array = {10,9,2,5,3,7,101,18};
        System.out.println(findLengthOfLCIS(array));
    }

    //快慢指针
    public static int findLengthOfLCIS(int[] nums) {
        if (nums.length == 0 || nums.length == 1) {
            return nums.length;
        }
        int slow = 0;
        int fast = 1;
        //ans为1是为了避免全部为2,2,2,2这种情况
        int ans = 1;
        for (int i = 0; i < nums.length; i++) {
            if (fast < nums.length && nums[fast] > nums[fast - 1]) {
                ans = Math.max(fast - slow + 1,ans);
                fast++;
            } else {
                slow = fast;
                fast++;
            }
        }
        return ans;
    }

    //贪心算法
    public static int findLengthOfLCIS2(int[] nums) {
        int ans = 0;
        int n = nums.length;
        int start = 0;
        for (int i = 0; i < n; i++) {
            if (i > 0 && nums[i] <= nums[i - 1]) {
                start = i;
            }
            ans = Math.max(ans, i - start + 1);
        }
        return ans;
    }

}
